package com.upc.toss.slice;

import com.upc.toss.ResourceTable;
import com.upc.toss.util.Get;
import com.upc.toss.util.Update;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.Revocable;
import ohos.app.dispatcher.task.TaskPriority;

public class SecondAbilitySlice extends AbilitySlice{


    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_second);

        TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);

        Text textSTemperature=(Text) findComponentById(ResourceTable.Id_text_tem_warning);
        Text textSHumanity=(Text) findComponentById(ResourceTable.Id_text_hum_warning) ;
        Text textSLuminance=(Text) findComponentById(ResourceTable.Id_text_lum_warning) ;
        textSTemperature.setText(Get.settingTemperature);
        textSHumanity.setText(Get.settingHumidity);
        textSLuminance.setText(Get.settingLuminance);

        TextField textSetTep =(TextField) findComponentById(ResourceTable.Id_text_tem_setting);
        TextField textSetHum=(TextField) findComponentById(ResourceTable.Id_text_hum_setting);
        TextField textSetLum=(TextField) findComponentById(ResourceTable.Id_text_lux_setting);


        Button buttonTmp =(Button) findComponentById(ResourceTable.Id_button_tem_setting);
        Button buttonHum=(Button) findComponentById(ResourceTable.Id_button_hum_setting);
        Button buttonLum=(Button) findComponentById(ResourceTable.Id_button_lux_setting);

        buttonTmp.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                textSTemperature.setText(textSetTep.getText());
                Revocable revocable = globalTaskDispatcher.asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        Update.postCommandValue("Set_Temperature","Setting_Temperature",textSetTep.getText());
                    }
                });
            }
        });
        buttonHum.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                textSHumanity.setText(textSetHum.getText());
                Revocable revocable = globalTaskDispatcher.asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        Update.postCommandValue("Set_Humidity","Setting_Humidity",textSetHum.getText());
                    }
                });
            }
        });
        buttonLum.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                textSLuminance.setText(textSetLum.getText());
                Revocable revocable = globalTaskDispatcher.asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        Update.postCommandValue("Set_Luminance","Setting_Luminance",textSetLum.getText());
                    }
                });
            }
        });



        TabList tabList = (TabList) findComponentById(ResourceTable.Id_tab_list_second);
        TabList.Tab tab1 = tabList.new Tab(getContext());
        tab1.setText("查看");
        tabList.addTab(tab1);
        tab1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                present(new MainAbilitySlice(),new Intent());
            }
        });

        TabList.Tab tab2 = tabList.new Tab(getContext());
        tab2.setText("报警");
        tabList.addTab(tab2);
        tab2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                present(new SecondAbilitySlice(),new Intent());
            }
        });

        Switch btnSwitchLight = (Switch) findComponentById(ResourceTable.Id_btn_switch_light);

        final boolean[] tempIfLightOn = new boolean[1];
        tempIfLightOn[0] = false;

        btnSwitchLight.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            // 回调处理Switch状态改变事件
            @Override
            public void onCheckedChanged(AbsButton button, boolean isChecked) {
                Revocable revocable = globalTaskDispatcher.asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        if(tempIfLightOn[0]){
                            Update.postCommand("Control_Light","Light","OFF");
                            tempIfLightOn[0] =false;
                        }
                        else{
                            Update.postCommand("Control_Light","Light","ON");
                            tempIfLightOn[0] =true;
                        }
                    }
                });
            }
        });

        Switch btnSwitchMotor = (Switch) findComponentById(ResourceTable.Id_btn_switch_motor);

        final boolean[] ifMotorOn = new boolean[1];
        ifMotorOn[0] = false;

        btnSwitchMotor.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            // 回调处理Switch状态改变事件
            @Override
            public void onCheckedChanged(AbsButton button, boolean isChecked) {
                Revocable revocable = globalTaskDispatcher.asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        if(ifMotorOn[0]){
                            Update.postCommand("Control_Motor","Motor","OFF");
                            ifMotorOn[0] =false;
                        }
                        else{
                            Update.postCommand("Control_Motor","Motor","ON");
                            ifMotorOn[0] =true;
                        }
                    }
                });
            }
        });

    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}